<template>
  <div>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-s-promotion"></i> 后台管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/zwt' }">账号管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="padding: 10px">
      <div>
        <el-button type="primary" @click="add">新增<i class="el-icon-circle-plus-outline"></i></el-button>
        <el-button type="primary" @click="exp">导出<i class="el-icon-top"></i></el-button>
      </div>

      <div style="margin: 10px 0">
        <el-input v-model="search" placeholder="请输入搜索关键字" style="width: 20%" clearable></el-input>
        <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
      </div>
      <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%">
        <el-table-column
            prop="id"
            label="id" align="center"  width="60"
            sortable>
        </el-table-column>
        <el-table-column label="头像" width="100" align="center">
          <template slot-scope="scope">
            <el-avatar :size="30" :src="scope.row.imgUrl"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="用户名"  header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="username" label="昵称"  header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="phoneNum" label="手机号码"  align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sfzNum" label="身份证号"  align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="gender" label="性别"  align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" size="small" type="success">编辑<i class="el-icon-edit"></i></el-button>
            <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button slot="reference" type="danger" size="small">删除<i class="el-icon-remove-outline"></i></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
          <el-form :model="form" label-width="120px">
            <el-form-item label="id">
              <el-input v-model="form.id" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item label="用户名">
              <el-input v-model="form.name" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model="form.username" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item label="手机号码">
              <el-input v-model="form.phoneNum" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item label="身份证号">
              <el-input v-model="form.sfzNum" style="width: 80%"></el-input>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio v-model="form.gender" label="男">男</el-radio>
              <el-radio v-model="form.gender" label="女">女</el-radio>
              <el-radio v-model="form.gender" label="未知">未知</el-radio>
            </el-form-item>
            <el-form-item label="头像">
              <el-input v-model="form.imgUrl" style="width: 80%"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save()">确 定</el-button>
  </span>
        </el-dialog>
      </div>

    </div>

  </div>
</template>
<script>

import axios from "axios";
import request from "@/utils/request";


export default {
  name:'UserManagement',
  components:{

  },
  data(){
    return{
      form:{},
      dialogVisible:false,
      search:'',
      currentPage:1,
      pageSize:10,
      total:0,
      tableData:[
      ]
    }
  },
  created(){
    this.load()

  },
  methods:{
    load(){
      request.get("http://localhost:29666/user/select",{
        params:{
          pageNum:this.currentPage,
          pageSize:this.pageSize,
          search:this.search
        }
      }).then(res=>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },
    add(){
      this.dialogVisible = true
      this.form = {}
    },
    save() {
      if (this.form.id) {
        // 更新操作
        request.put("http://localhost:29666/user/update", this.form).then(res => {
          if (res.code == '0') {
            this.$message({
              type: "success",
              message: "更新成功"
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load();
          this.dialogVisible = false;
        });
      } else {
        // 新增操作
        request.post("http://localhost:29666/user/add", this.form).then(res => {
          if (res.code == '0') {
            this.$message({
              type: "success",
              message: "新增成功"
            });
          } else {
            this.$message({
              type: "error",
              message: res.msg
            });
          }
          this.load();
          this.dialogVisible = false;
        });
      }
    }
    ,
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.dialogVisible=true

    },
    handleDelete(id){
      console.log(id)
      request.delete("http://localhost:29666/user/delete/"+id).then(res=>{
        if (res.code == '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          });
        } else {
          this.$message({
            type: "error",
            message: res.msg
          });
        }
        this.load()

      })
    },
    handleSizeChange(pageSize){
      this.pageSize=pageSize
      this.load()

    },
    handleCurrentChange(pageNum){
      this.currentPage=pageNum
      this.load()
    },
    exp(){
      window.open("http://localhost:29666/user/export")
    }


  }
}
</script>


